/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystem;

import java.io.File;
import java.util.regex.Pattern;

/**
 *
 * @author Tomas
 */
public class FileSystemUtils {

    public static FileNode findFileNode(String path, FileNode root) {        
        return findFileParentNode(path.split(Pattern.quote(File.separator)),root, false);
    }

    public static FileNode findParentNode(String path, FileNode root) {
        return findFileParentNode(path.split(Pattern.quote(File.separator)),root, true);
    }

    public static FileNode findFileNode(String[] path, FileNode root) {
        return findFileParentNode(path, root, false);
    }

    public static FileNode findParentNode(String[] path, FileNode root) {
        return findFileParentNode(path, root,true);
    }

    private static FileNode findFileParentNode(String[] path, FileNode root, boolean parent) {        
        FileNode findNode = root;
        int lenght;
        if (parent) {
            lenght = path.length - 1;
        } else {
            lenght = path.length;
        }
        boolean nodeFinded = false;
        for (int i = 0; i < lenght; i++) {
            String string = path[i];
            nodeFinded = false;
            for (FileNode fileNode : findNode.getChilds()) {
                if (fileNode.getName().equals(string)) {
                    findNode = fileNode;
                    nodeFinded = true;
                    break;
                }
            }
            if (!nodeFinded) {
                return findNode;
            }
        }
        return findNode;
    }
}
